<template>
  <el-card shadow="never">
    <el-button class="mb20" @click="$router.push({ name: 'evaluateStartTest' })">返回</el-button>
    <el-tabs class="classify-tabs" v-model="name">
      <el-tab-pane label="事件" name="add">
        <el-row :gutter="20">
          <el-col :span="16">
            <create-test
              @notSubmit="handleNotSubmit"
              @postParam="handlePostParam"
              @showDialog="handleShowDialog"
              ref="createTest"
            />
          </el-col>
          <el-col :span="8">
            <create-image @postParam="handleImageParam" ref="createImage" />
          </el-col>
        </el-row>
        <div class="tl-center">
          <el-button class="mr20" @click="handleBack">取消</el-button>
          <!-- <el-button class="mr20">重置</el-button> -->
          <el-button type="primary" @click="handleSubmit">提交</el-button>
        </div>
      </el-tab-pane>
      <el-tab-pane label="本周期内历史事件" name="history">
        <history-comp ref="historyComp" />
      </el-tab-pane>
    </el-tabs>
    <create-modal @postTable="handlePostTable" ref="createModal" />
  </el-card>
</template>

<script>
import { getSession, setSession, removeSession, openNotification } from '@/utils/util'
import CreateTest from './CreateTest'
import CreateModal from './Modal'
import CreateImage from './CreateImage'
import HistoryComp from './HistoryComp'
import { eventCreate } from '@/api/eval'

export default {
  components: {
    CreateTest,
    CreateModal,
    CreateImage,
    HistoryComp
  },
  data() {
    return {
      noError: true,
      mdl: this.$route.params.item || getSession('createTestItem'),
      submitParam: {
        evidencePhotoList: null
      },
      name: 'add'
    }
  },
  methods: {
    handleNotSubmit() {
      this.noError = false
    },
    // modal 传递数据显示表格
    handlePostTable(obj) {
      this.$refs.createTest.showTable(obj)
    },
    handlePostParam(obj) {
      this.submitParam = Object.assign(this.submitParam, { ...obj, evalItemId: this.mdl.evalItemId })
      this.noError = true
    },
    handleImageParam(str) {
      this.$set(this.submitParam, 'evidencePhotoList', str)
    },
    handleShowDialog(obj) {
      this.$refs.createModal.show(obj)
    },
    handleBack() {
      this.$router.push({
        name: 'evaluateStartTest'
      })
    },
    async handleSubmit() {
      this.$refs.createTest.getParam()
      this.$refs.createImage.getParam()

      if (this.noError) {
        const res = await eventCreate(this.submitParam)
        if (res.state === '000000') {
          openNotification('success', '新增事件成功')
          this.$router.push({
            name: 'evaluateStartTest'
          })
        }
      }
    }
  },
  created() {
    if (this.$route.params !== '{}') {
      setSession('createTestItem', this.$route.params.item)
    }
    if (this.mdl) {
      this.$nextTick(() => {
        this.$refs.createTest.show(this.mdl)
      })
    } else {
      this.$message.error('页面发生错误，即将返回列表页')
      this.$router.push({
        name: 'evaluateStartTest'
      })
    }
  },
  destroyed() {
    removeSession('tabName')
    removeSession('createTestItem')
  }
}
</script>
